import { OneModal } from '@/components/one-ui';
import { Button } from 'antd';
import { Ref, useContext, useRef } from 'react';

// import modalStyles from '../addEditInfoSty.less';
import MyIcon from '@/components/MyIcon';
import { IOneModalRef } from '@/pages/type';
import { IConfirmModalRange } from '../type';
import { ResourcesContext } from '../hooks';
import { ResourRangeContext } from '../hooks/useResourRangeContext';

export const ConfirmModalRange = ({
  onClose,
  visible,
  onReload, //刷新管理页面
  chanageSwitch,
  tableRowObj,
}: // leftRowCheck
IConfirmModalRange) => {
  const confirmRef: Ref<IOneModalRef> = useRef(null); //弹框的ref
  const { resourCurrent } = useContext(ResourcesContext); //获取新增是 数据范围还是 功能操作  1 为数据范围, 资源类型只能选 2, 2为功能操作,资源类型只能选 3
  const { checkTableRows } = useContext(ResourRangeContext);

  // 关闭modal
  const onCloseModal = () => {
    if (confirmRef) {
      if (confirmRef.current) {
        if (confirmRef.current.onModalCancel) {
          confirmRef.current.onModalCancel();
        }
      }
    }
  };

  // 点击按钮调用接口
  const clickOnOk = () => {
    if (tableRowObj?.pIsOpen == 0) {
      onCloseModal();
    } else {
      chanageSwitch(false, tableRowObj);
      onCloseModal();
    }
  };

  // 处理提示信息
  const getTipStr = () => {
    let tip = `禁用后，该${
      resourCurrent == 1 ? '数据范围' : resourCurrent == 2 ? '功能操作' : ''
    }及其下属${
      resourCurrent == 1 ? '数据范围' : resourCurrent == 2 ? '功能操作' : ''
    }均会级联关闭和禁用，是否确认禁用？`;
    if (!checkTableRows?.url && checkTableRows?.method == 'ALL') {
      if (tableRowObj?.pIsOpen == 0) {
        tip = `对不起，该${
          resourCurrent == 1 ? '数据范围' : resourCurrent == 2 ? '功能操作' : ''
        }所属资源目录已禁用，请先启用所属资源目录！`;
      }
    } else {
      if (tableRowObj?.pIsOpen == 0) {
        tip = `对不起，该${
          resourCurrent == 1 ? '数据范围' : resourCurrent == 2 ? '功能操作' : ''
        }上级${
          resourCurrent == 1 ? '数据范围' : resourCurrent == 2 ? '功能操作' : ''
        }已禁用，请先启用上级${
          resourCurrent == 1 ? '数据范围' : resourCurrent == 2 ? '功能操作' : ''
        }！`;
      }
    }
    return tip;
  };

  return (
    <>
      <OneModal
        ref={confirmRef}
        open={true}
        getContainer={false}
        title={
          <div style={{ display: 'flex', alignItems: 'center' }}>
            <span
              style={{ marginRight: 10, fontSize: '24px', color: '#faad14' }}
            >
              <MyIcon type="icon-info-circle" />
            </span>
            <span>
              {`禁用${
                resourCurrent == 1
                  ? '数据范围'
                  : resourCurrent == 2
                  ? '功能操作'
                  : '提示'
              }`}
            </span>
          </div>
        }
        width={document.body.clientWidth * 0.2}
        onCancel={onClose}
        footer={
          <>
            <div
            // className={modalStyles['dept-update-btn']}
            >
              <Button onClick={onCloseModal}>取消</Button>
              <Button type={'primary'} onClick={clickOnOk}>
                确定
              </Button>
            </div>
          </>
        }
        bodyStyle={{
          padding: '15px 30px',
        }}
      >
        <div style={{ fontSize: '18px', color: '#ff000d' }}>{getTipStr()}</div>
      </OneModal>
    </>
  );
};
